{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "import urllib\n",
    "import random\n",
    "import math\n",
    "import time\n",
    "f = open(\"C:/Users/user/Desktop/datasets/kaggle/track1/track1/user_sns.txt\",\"r\")\n",
    "# f = open(\"C:\\Users\\user\\Desktop\\datasets\\soc-LiveJournal1.txt\",\"r\")\n",
    "# f = open(\"C:\\Users\\user\\Desktop\\datasets\\twitter-2010.txt\",\"r\")\n",
    "\n",
    "vertexDic = {}  # {vertex:degree, ,,,}\n",
    "edges = []\n",
    "\n",
    "# sortVertices = sorted(vertexDic.items(), key=lambda e:e[1], reverse = True)\n",
    "\n",
    "# precent = 1\n",
    "# Cut = len(sortVertices) / (100 / precent)\n",
    "# BigestV = sortVertices[0:Cut]\n",
    "# BigestVertex = []\n",
    "# for i in range(Cut):\n",
    "#     BigestVertex.append(BigestV[i][0])\n",
    "\n",
    "# edgeNum = 0\n",
    "# for i in range(len(edges)):\n",
    "#     src = edges[i][0]\n",
    "#     tar = edges[i][1]\n",
    "#     if src in BigestVertex or tar in BigestVertex:\n",
    "#         edgeNum = edgeNum + 1\n",
    "\n",
    "# print edgeNum / 1.0 / len(edges)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "for line in f:\n",
    "    srcTar = line.strip().split()\n",
    "    if srcTar[0] == '#':\n",
    "        continue\n",
    "    src = long(srcTar[0])\n",
    "    tar = long(srcTar[1])\n",
    "    edges.append((src, tar))\n",
    "    if vertexDic.has_key(src):\n",
    "        vertexDic[src] = vertexDic[src] + 1\n",
    "    else:\n",
    "        vertexDic[src] = 1\n",
    "    if vertexDic.has_key(tar):\n",
    "        vertexDic[tar] = vertexDic[tar] + 1\n",
    "    else:\n",
    "        vertexDic[tar] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50655143\n",
      "1944589\n"
     ]
    }
   ],
   "source": [
    "print len(edges)\n",
    "print len(vertexDic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print len(vertexDic)\n",
    "sortVertices = sorted(vertexDic.items(), key=lambda e:e[1], reverse = True)\n",
    "# print sortVertices[0]\n",
    "ans = []\n",
    "for i in range(30000):\n",
    "    ans.append(0)\n",
    "for key in vertexDic:\n",
    "#     print str(key) + \"   \" + str(vertexDic[key])\n",
    "    ans[vertexDic[key]] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10009L, 22889)\n",
      "(37356L, 15554)\n",
      "(87L, 15218)\n",
      "(10029L, 14029)\n",
      "(8737L, 13439)\n",
      "(18964L, 13183)\n",
      "(2914L, 12422)\n",
      "(18963L, 11861)\n",
      "(91341L, 9787)\n",
      "(40748L, 9312)\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print sortVertices[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4550814 0.938782330367\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "for i in range(101):\n",
    "    a += ans[i]\n",
    "print a, a/1.0/4847571"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "highDegVerticesSet = set()\n",
    "for i in range(100000):\n",
    "    highDegVerticesSet.add(sortVertices[i][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "highDegAll = 0\n",
    "DegAll = 0\n",
    "for i in range(100000):\n",
    "    highDegAll += sortVertices[i][1]\n",
    "for i in range(4847570):\n",
    "    DegAll += sortVertices[i][1]\n",
    "print highDegAll\n",
    "print DegAll\n",
    "print highDegAll/1.0/DegAll"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "high4DegVerticesSet = set()\n",
    "for i in range(3000000, 3100000):\n",
    "    high4DegVerticesSet.add(sortVertices[i][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "high4DegAll = 0\n",
    "DegAll = 0\n",
    "for i in range(3000000, 3100000):\n",
    "    high4DegAll += sortVertices[i][1]\n",
    "for i in range(4847570):\n",
    "    DegAll += sortVertices[i][1]\n",
    "print high4DegAll\n",
    "print DegAll\n",
    "print high4DegAll/1.0/DegAll"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "fansortVertices = sorted(vertexDic.items(), key=lambda e:e[1], reverse = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "vertexDeg1 = set()\n",
    "vertexDeg2 = set()\n",
    "vertexDeg3 = set()\n",
    "vertexDeg4 = set()\n",
    "vertexDeg5 = set()\n",
    "vertexDeg6 = set()\n",
    "vertexDeg7 = set()\n",
    "vertexDeg8 = set()\n",
    "vertexDeg9 = set()\n",
    "vertexDeg10 = set()\n",
    "for i in range(4847570):\n",
    "    if(fansortVertices[i][1] == 1):\n",
    "        vertexDeg1.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 2):\n",
    "        vertexDeg2.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 3):\n",
    "        vertexDeg3.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 4):\n",
    "        vertexDeg4.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 5):\n",
    "        vertexDeg5.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 6):\n",
    "        vertexDeg6.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 7):\n",
    "        vertexDeg7.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 8):\n",
    "        vertexDeg8.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 9):\n",
    "        vertexDeg9.add(fansortVertices[i][0])\n",
    "    if(fansortVertices[i][1] == 10):\n",
    "        vertexDeg10.add(fansortVertices[i][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "113709\n",
      "107038\n",
      "1.06232366076\n",
      "0.106232366076\n"
     ]
    }
   ],
   "source": [
    "ans = 0\n",
    "for i in range(len(edges)):\n",
    "    if edges[i][0] in vertexDeg10 and edges[i][1] in highDegVerticesSet:\n",
    "        ans = ans + 1\n",
    "    if edges[i][1] in vertexDeg10 and edges[i][0] in highDegVerticesSet:\n",
    "        ans = ans + 1\n",
    "print ans\n",
    "print len(vertexDeg10)\n",
    "print ans/1.0/len(vertexDeg10)\n",
    "print ans/10.0/len(vertexDeg10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8210\n",
      "107038\n",
      "0.0767017320951\n",
      "0.00767017320951\n"
     ]
    }
   ],
   "source": [
    "ans = 0\n",
    "for i in range(len(edges)):\n",
    "    if edges[i][0] in vertexDeg10 and edges[i][1] in high4DegVerticesSet:\n",
    "        ans = ans + 1\n",
    "    if edges[i][1] in vertexDeg10 and edges[i][0] in high4DegVerticesSet:\n",
    "        ans = ans + 1\n",
    "print ans\n",
    "print len(vertexDeg10)\n",
    "print ans/1.0/len(vertexDeg10)\n",
    "print ans/10.0/len(vertexDeg10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
